package de.hauck.rtmidlet.views;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.model.StockStore;

public class CanvasSplash extends Canvas implements CommandListener {
	private Image splash;
	private Font f_small, f_med;
	private int slideType = 0;
	private StockStore myStore;
	private boolean _withStatus;
	private String _status=""; 

	public CanvasSplash() {
		setFullScreenMode(true);
		setCommandListener(this);
		try {
			splash = Image.createImage("/icon.gif");
		} catch (IOException e) {
			e.printStackTrace();
		}
		f_small = Font.getFont(Font.FONT_STATIC_TEXT, Font.STYLE_PLAIN,
				Font.SIZE_SMALL);
		f_med = Font.getFont(Font.FONT_STATIC_TEXT, Font.STYLE_PLAIN,
				Font.SIZE_MEDIUM);
	}

	protected void paint(Graphics g) {
		g.setColor(0);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(0xFFFFFF);
		if (slideType == 0) {
			if (splash!=null) {
			g.setFont(f_med);
			g.drawImage(splash, getWidth() / 2, (getHeight() / 2)
					- splash.getHeight(), Graphics.TOP | Graphics.HCENTER);
			g.drawString(Configuration.VERSION, getWidth() / 2,
					(getHeight() / 2) - splash.getHeight() - 10,
					Graphics.BOTTOM | Graphics.HCENTER);
			g.drawString(Configuration.AUTHOR, getWidth() / 2,
					(getHeight() / 2) + splash.getHeight() / 2, Graphics.TOP
							| Graphics.HCENTER);
			g.setFont(f_small);
			g.drawString(_status, getWidth() / 2,
					(getHeight()-f_small.getHeight()-3), Graphics.TOP
							| Graphics.HCENTER);
			}
		} else if (slideType == 1) {
			if (myStore==null) myStore = RTMidlet.getInstance().getStockStore();
			int ypos = 12;
			g.drawString("Statistics", getWidth() / 2, ypos, Graphics.TOP
					| Graphics.HCENTER);
			ypos += f_small.getHeight() + 5;
			g.setFont(f_small);
			g
					.drawString("Stocks total:", 5, ypos, Graphics.TOP
							| Graphics.LEFT);
			g.drawString(myStore.getFilteredStockVector(-1).size()+"", getWidth() - 5, ypos, Graphics.TOP | Graphics.RIGHT);
			ypos += f_small.getHeight();
			for (int i=0; i<Configuration.getAvailableChartTypes().length; i++) {
				g.drawString("Stocks "+Configuration.getAvailableChartTypes()[i]+":", 5, ypos, Graphics.TOP | Graphics.LEFT);
				g.drawString(myStore.getFilteredStockVector(i)
						.size()
						+ "", getWidth() - 5, ypos, Graphics.TOP | Graphics.RIGHT);
				ypos += f_small.getHeight();
			}
			
			//g.drawString("Stocks FT:", 5, ypos, Graphics.TOP | Graphics.LEFT);
			//g.drawString(myStore.getFilteredStockVector(Configuration.FILTER_CHART_FT)
			//		.size()
			//		+ "", getWidth() - 5, ypos, Graphics.TOP | Graphics.RIGHT);
			//ypos += f_small.getHeight();
			g.drawString("Stocks Yahoo:", 5, ypos, Graphics.TOP | Graphics.LEFT);
			g.drawString(myStore.getFilteredStockVector(Configuration.FILTER_OVERVIEW_YAHOO)
					.size()
					+ "", getWidth() - 5, ypos, Graphics.TOP | Graphics.RIGHT);
			ypos += f_small.getHeight();
			
			g.drawString("Download Session: ", 5, ypos, Graphics.TOP
					| Graphics.LEFT);
			g.drawString(Configuration.getDownloadCounter()/1024 + "KB", getWidth() - 5, ypos,
					Graphics.TOP | Graphics.RIGHT);
			ypos += f_small.getHeight();
			g.drawString("Download Total: ", 5, ypos, Graphics.TOP
					| Graphics.LEFT);
			g.drawString(Configuration.getDownloadSizeTotal() / 1024 + "KB",
					getWidth() - 5, ypos, Graphics.TOP | Graphics.RIGHT);
		} else if (slideType==2) {
			int ypos = 12;
			g.drawString("Help", getWidth() / 2, ypos, Graphics.TOP
					| Graphics.HCENTER);
			ypos += f_small.getHeight() + 5;	
			g.setFont(f_small);
			ypos+=f_small.getHeight();
			g.drawString("Here comes the help...", 5, ypos, Graphics.TOP | Graphics.LEFT);
			ypos+=f_small.getHeight();
			g.drawString("0 - zeros download counter", 5, ypos, Graphics.TOP | Graphics.LEFT);
			ypos+=f_small.getHeight();
			g.drawString("1 - reloads", 5, ypos, Graphics.TOP | Graphics.LEFT);
			ypos+=f_small.getHeight();
			g.drawString("4 - decreases period", 5, ypos, Graphics.TOP | Graphics.LEFT);
			ypos+=f_small.getHeight();
			g.drawString("6 - increases period", 5, ypos, Graphics.TOP | Graphics.LEFT);
			ypos+=f_small.getHeight();
			g.drawString("* - toggles debug", 5, ypos, Graphics.TOP | Graphics.LEFT);
			ypos+=f_small.getHeight();
			g.drawString("# - toggles overview", 5, ypos, Graphics.TOP | Graphics.LEFT);
			ypos+=f_small.getHeight();
			g.drawString("etc...", 5, ypos, Graphics.TOP | Graphics.LEFT);
			
		}
	}

	public void commandAction(Command command, Displayable dble) {
	}

	protected void keyPressed(int keyCode) {
		int action = getGameAction(keyCode);
		if (keyCode == KEY_NUM1) {
			this.repaint();
		} else if (keyCode == KEY_NUM0) {
			Configuration.setDownloadSizeTotal(0);
			Configuration.dirtyFlagConfig=true;
			this.repaint();
		} else if (action == LEFT) {
			if (_withStatus)
				if (slideType > 0)
					slideType--;
				else
					slideType = 2;
			this.repaint();
		} else if (action == RIGHT) {
			if (_withStatus)
				if (slideType < 2)
					slideType++;
				else
					slideType = 0;
			this.repaint();
		} else {
			_status="";
			RTMidlet.getInstance().showMenu();
		}
	}

	public void show(boolean withStatus) {
		_withStatus=withStatus;
		slideType=0;
		RTMidlet.getInstance().show(this);
	}
	
	public void showProgress(String status) {
		_status=status;
		this.repaint();
	}
}
